<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/25
 * Time: 10:42
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
class Report extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map = [];
            if(isset($banner_type)&&$banner_type!="")
            {
                $map['report_from'] = $banner_type;
            }
            if(isset($n_title)&&$n_title!="")
            {
                $map['n_title|h_title|report_content'] = ['like',"%" . $n_title . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('report')->alias('a')
                ->join('up_u_user uuu','uuu.user_id =a.report_user_id','left')
                ->join('up_news n','n.n_id = a.report_hot_id','left')
                ->join('up_hot uh','uh.h_id = a.report_hot_id','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('report')->alias('a')
                ->join('up_u_user uuu','uuu.user_id =a.report_user_id','left')
                ->join('up_news n','n.n_id = a.report_hot_id','left')
                ->join('up_hot uh','uh.h_id = a.report_hot_id','left')
                ->where($map)
                ->order('report_time desc')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uh.h_title,uh.h_id,n.n_id,n.n_title')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $rt_name = Db::name('report_type')->where('rt_id','in',$v['report_type_id'])->column('rt_name');
                    $lists[$k]['rt_name'] = implode($rt_name,',');
                    if($v['report_image']){
                        $lists[$k]['report_image'] = explode(',',$v['report_image']);
                    }
                    if($v['report_from'] == 2){
                        $lists[$k]['h_title'] = $v['n_title'];
                        $lists[$k]['h_id'] = $v['n_id'];
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 详情
     * @return mixed
     */
    public function detail(){
        $car_id = input('report_id');
        $detail = Db::table('up_report')
            ->field('report_content')
            ->where('report_id', $car_id)
            ->find();
        $this->assign('car', $detail);
        return $this->fetch('report/detail');
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('report')->where('report_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}